
#ifndef	MEMORY_ALLOCATOR_H
#define	MEMORY_ALLOCATOR_H


#include "tls.h"
#include "cache_lines.h"
#include "block_heaps.h"
#include "statistics.h"


namespace	memory
{
	class	allocator : public cache_line_listener
	{
	private:

		enum
		{
			lookaside_block_size		= 64,
			lookaside_shift_count		= 6,
			default_max_lookaside_size	= 16*1024,	// 16K
		};

	public:

		allocator();
		~allocator();

		bool	create( bool statistics = true, size_t parallelism = 0, size_t max_lookaside_size = default_max_lookaside_size );
		void	destroy();

		void*	alloc( size_t size );
		bool	free( void* p );

		const statistics*	get_statistics()	const;

	private:

		bool	must_be_free( void* entry );

		void	on_alloc( size_t line_idx, size_t las_idx );
		void	on_free( size_t line_idx, size_t las_idx );
		void	on_delete( SLIST_ENTRY* entry );

	private:

		common::tls	hint_;

		cache_lines	caches_;
		block_heaps	heaps_;

		statistics	statistics_;

	};
}



#endif